<!--
 * @Author: shenya
 * @Date: 2023-09-06 16:51:54
 * @Description: 规则管理
-->
<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDelete"
    ></avue-crud>
  </basic-container>
</template>
<script>
import crudMixins from '@/mixins/crud';
import { tableOption } from '@/const/crud/rules/rulesManage';
import {
  selectDisplayFile,
  deleteRulesById,
  selectRulesById,
  updateRules,
} from '@/api/core/rules';
export default {
  mixins: [crudMixins],
  data() {
    return {
      tableLoading: false,
      form: {},
      tableOption: tableOption(),
    };
  },
  methods: {
    // 保存
    async handleSave(row, done, loading) {
      // try {
      //   const params = await this.getParams(row);
      //   await addObj(params);
      //   done();
      //   this.getList();
      // } catch (error) {
      //   loading();
      //   throw new Error(error);
      // }
    },
    // 编辑
    async handleUpdate(row, index, done, loading) {
      const content = this.form.content ? this.form.content : row.content;
      const params = {
        id: Number(row.id),
        name: this.form.name ? this.form.name : row.name,
        content,
      };
      try {
        await updateRules(params);
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    handleDelete: function (row, index) {
      this.$confirm(
        '删除后将影响内容在移动端的展示，请您谨慎删除，确定删除吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return deleteRulesById(row.id);
        })
        .then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
    },
    // 打开窗口前的拦截，针对编辑需要进行调用接口回显数据
    async beforeOpen(done, type) {
      // 如果不是新增
      if (type !== 'add') {
        const { data } = await selectRulesById(this.form.id);
        this.form = data.data;
      }
      done();
    },
    // 获取列表
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      selectDisplayFile(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped></style>
